package com.stylefeng.guns.modular.system.api;

import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.core.util.IpUtil;
import com.stylefeng.guns.core.util.NetworkUtil;
import com.stylefeng.guns.modular.system.model.Jaruse;
import com.stylefeng.guns.modular.system.service.IJaruseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class JaruseApiController {

    @Autowired
    IJaruseService iJaruseService;

    //返回包的使用情况
    @RequestMapping("/jaruse")
    public JsonResult jarInformation(HttpServletRequest request){
        Jaruse jaruse = null;
        String ip = null;
        try {

            ip = NetworkUtil.getIpAddress(request);

            jaruse = new Jaruse();
            jaruse.setChannel(request.getParameter("channel"));
            jaruse.setJarName(request.getParameter("jarName"));
            jaruse.setIsUse(Integer.parseInt(request.getParameter("isUse")));
            jaruse.setJarVersion(Integer.parseInt(request.getParameter("jarVersion")));
            jaruse.setReqip(ip);
            jaruse.setCountry(IpUtil.address(ip,0));
            jaruse.setProvince(IpUtil.address(ip,2));
            jaruse.setModel(request.getParameter("model"));
            jaruse.setOpeartor(request.getParameter("operator"));
            jaruse.setOs(request.getParameter("os"));
            jaruse.setUdid(request.getParameter("udid"));
            jaruse.setImsi(request.getParameter("imsi"));
            jaruse.setImei(request.getParameter("imei"));
            jaruse.setMac(request.getParameter("mac"));
            iJaruseService.insert(jaruse);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            return ResultGenerator.genFailResult(e.getMessage());
        }finally {
            jaruse = null;
        }
    }



}
